<!--
    Test it by right-click -> Run-file (shift-F6)
    Just a test of how to get information from a database, test it by filling in data and 
    push submit button, then will all the airport get listed.
    
    String url should be changed to the database that one want to use,
    also change the username and password.
    If your database is a oracle database then should the driver also be changed. 
-->

<%@ page language="java" import="java.sql.*" %>
<%
    String driver = "com.mysql.jdbc.Driver";        //mysql database driver
//  String driver = "oracle.jdbc.OracleDriver";     //oracle database driver
    
    Class.forName(driver).newInstance();

    Connection con=null;
    ResultSet rst=null;		
    Statement stmt=null;

    try{
        String url="jdbc:mysql://localhost/project";
        String username = "project";
        String password = "nice";
        con=DriverManager.getConnection(url,username,password);
	stmt=con.createStatement();
    }catch(Exception e){
	System.out.println(e.getMessage());
    }
    
    if(request.getParameter("action") != null){  

        String bookname=request.getParameter("bookname");
        String author=request.getParameter("author");	
	rst=stmt.executeQuery("select * from airport");
        ResultSetMetaData res = rst.getMetaData();
        
      %>

<html>
   <body>
   <center><h2>List of Airports</h2>
   <table border="1" cellspacing="0" cellpadding="0">
      
    <tr>
        <% for (int i = 1; i < res.getColumnCount(); i++) { %>
        <td><b><%=res.getColumnLabel(i)%></b></td>
        <%}%>
    </tr>
    <%
  
        while(rst.next()){
             System.out.println(rst.getString(1));
    %>

    <tr>
        <% for (int i = 1; i < res.getColumnCount(); i++) { %>
        <td><%=rst.getString(i)%></td>    
        <%}%>
    </tr>
    <%    
        }
        rst.close();
        stmt.close();
	con.close();
            			
    %>
   </table>
   </center>
   </body>
<html>
<%}else{%>

<html>
    <head>
	<title>Get Airports</title>
        <script language="javascript">
            function validate(objForm){
                if(objForm.bookname.value.length==0){
                    alert("Please enter  Book Name!");
                    objForm.bookname.focus();
                    return false;
		}		
                if(objForm.author.value.length==0){
                    alert("Please enter Author name!");
                    objForm.author.focus();
                    return false;
		}
                return true;
            }
        </script>
    </head>
    <body>
        <center>
        <form action="Databasetest.jsp" method="post" name="entry" onSubmit="return validate(this)">
        <input type="hidden" value="list" name="action">
            <table border="1" cellpadding="0" cellspacing="0">
                <tr>
                    <td>
                        <table>
                            <tr><td colspan="2" align="center"><h2>Book Entry Form</h2></td></tr>
                            <tr><td colspan="2">&nbsp;</td></tr>
                            <tr><td>Book Name:</td><td><input name="bookname" type="text" size="50"></td></tr>
                            <tr><td>Author:</td><td><input name="author" type="text" size="50"></td></tr>
                            <tr><td colspan="2" align="center"><input type="submit" value="Submit"></td></tr>
                        </table>
                    </td>
                </tr>
            </table>
	</form>
	</center>
    </body>
</html>

<%}%>



